package com.ggkt.persistence.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 *
 * @Desc
 * @author ggkt
 * @copyright 硅谷课堂
 * 这是一个springboot项目代码生成器
 * @date 2020/4/21
 */
public class MyBatisGenerator {
    public static String author="ggkt";
    //文件生成位置
    public static String ourDir="D:\\codeGen";
    public static void main(String[] args) {
        //生成代码所属包
        String packageName = "com.ggkt.account";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName,"arr_area");
    }

    /**
     *
     * @param serviceNameStartWithI 是否以I开头 user -> UserService, 设置成true: user -> IUserService
     * @param packageName 包名
     * @param tableNames 需要生成的表数组
     */
    private static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        int i = 0;
        switch (i) {
            case 0:
                dataSourceConfig.setDbType(DbType.MYSQL)
                        .setUrl("jdbc:mysql://192.168.*.*:6006/arrange?autoReconnect=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf8;useSSL=false;")
                        .setUsername("******")
                        .setPassword("******")
                        .setDriverName("com.mysql.jdbc.Driver");
                break;
            case 1:
                dataSourceConfig.setDbType(DbType.MYSQL)
                        .setUrl("jdbc:mysql://192.168.*.*:6006/attendance?autoReconnect=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf8;useSSL=false;")
                        .setUsername("******")
                        .setPassword("******")
                        .setDriverName("com.mysql.jdbc.Driver");
                break;
            case 2:
                dataSourceConfig.setDbType(DbType.SQL_SERVER)
                        .setUrl("jdbc:sqlserver://192.168.*.*:1433;DatabaseName=AccJiaowu")
                        .setUsername("******")
                        .setPassword("******")
                        .setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                break;
            case 3:
                dataSourceConfig.setDbType(DbType.MYSQL)
                        .setUrl("jdbc:mysql://192.168.*.*:3306/bbs?autoReconnect=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf8;useSSL=false;serverTimezone=UTC;")
                        .setUsername("******")
                        .setPassword("******")
                        .setDriverName("com.mysql.jdbc.Driver");
                break;
            default:
                break;
        }

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                //需要生成代码的表前缀(生成时java类不包含在此声明的前缀)
                .setTablePrefix(new String[]{"ctt_","sys_","msg_","fa_","RAD_ADMIN_","vote_","ad_","ats_"})
                .setCapitalMode(true)
                .setEntityLombokModel(true)
//                .setDbColumnUnderline(true)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames);
        //修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(false)
                .setAuthor(author)
                .setOutputDir(ourDir)
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        TemplateConfig templateConfig=new TemplateConfig();
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setTemplate(templateConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("domain")
                                .setMapper("mapper")
                                .setXml("mapper")
                ).execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}
